package com.company.LinkList;

import sun.awt.image.ImageWatched;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}23:10
 */
public class LinkedAndLinked {
    public static void main(String[] args) {

    }


    public static class LinkedDemo{
        LinkedBean head = new LinkedBean("a",1);

        public void addDemo(LinkedBean linkedBean){
            LinkedBean bean = head;
            while (true){
                if (bean.next==null){
                    System.out.println("为空");
                    break;
                }
                bean = bean.next;
            }
            bean.next = linkedBean;
        }

        public void list(){
            LinkedBean bean = head.next;
            if (bean.next==null){
                System.out.println("此链表为空");
            }
            while (true){
                if (bean.next==null){
                    System.out.println("链表已到底");
                    break;
                }
                System.out.println(bean.name);
                System.out.println(bean.no);
                bean = bean.next;
            }
        }

        public LinkedBean mergeTwoLists(LinkedBean bean1,LinkedBean bean2){
            if (bean1==null){
                return bean1;
            }
            if (bean2==null){
                return bean2;
            }
            //开始递归
            LinkedBean newBean = null;
            if (bean1.no>bean2.no){
                newBean = bean2;
                newBean.next = mergeTwoLists(bean1,bean2.next);
            }
            if (bean1.no<bean2.no){
                newBean = bean1;
                newBean.next = mergeTwoLists(bean1.next,bean2);
            }
            return newBean;
        }
    }

    static class LinkedBean{
        private String name;
        private int no;
        LinkedBean next;

        public LinkedBean(String name, int no) {
            this.name = name;
            this.no = no;
        }

        @Override
        public String toString() {
            return "LinkedBean{" +
                    "name='" + name + '\'' +
                    ", no=" + no +
                    '}';
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getNo() {
            return no;
        }

        public void setNo(int no) {
            this.no = no;
        }
    }
}
